//
//  HomeClassificationViewController.m
//  UI_A项目
//
//  Created by dlios on 15-1-16.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "HomeClassificationViewController.h"

@interface HomeClassificationViewController ()

@end

@implementation HomeClassificationViewController

- (void)dealloc
{
    [_segmented release];
    [_popularTableView1 release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.model.tname;
    
    [self AFN];
    self.headerImageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=3817649434,2082281439&fm=21&gp=0.jpg"]];
    self.headerImageview.frame = CGRectMake(0, 64, 375, 200);
    [self.view addSubview:self.headerImageview];
    [_headerImageview release];
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"最火", @"最近更新", @"经典", nil];
    self.segmented = [[UISegmentedControl alloc] initWithItems:arr];
    self.segmented.frame = CGRectMake(0, 210, 375, 40);
    [self.segmented setTintColor:[UIColor blackColor]];
    [self.view addSubview:self.segmented];
    self.segmented.selectedSegmentIndex = 0;
    [self.segmented addTarget:self action:@selector(segmented:) forControlEvents:UIControlEventValueChanged];
    [self.segmented release];
    
    self.popularTableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, 375, 380) style:UITableViewStylePlain];
    
    //    self.popularTableView1.rowHeight = 180;
    self.popularTableView1.alpha = 0.9;
    [self.view addSubview:self.popularTableView1];
    self.popularTableView1.delegate = self;
    self.popularTableView1.dataSource = self;

    
    /*
    [self.popularTableView1 registerClass:[ClassTableViewCell class] forCellReuseIdentifier:@"jiejie"];
    [self.popularTableView1 addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    [self.popularTableView1 headerBeginRefreshing];
    
    
    //    上拉加载更多（进入刷新状态就会调用self 的  footerRereshing）
    [self.popularTableView1 addFooterWithTarget:self action:@selector(footerRereshing)];
    
    
    
    
    
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.popularTableView1.headerPullToRefreshText = @"下拉可以刷新了";
    self.popularTableView1.headerReleaseToRefreshText = @"松开马上刷新了";
    self.popularTableView1.headerRefreshingText = @"喵 ~ ";
    
    self.popularTableView1.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.popularTableView1.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.po.footerRefreshingText = @"喵";
    
    self.previous_cursor = [NSMutableArray array];
    NSString *str = @"0";
    [self.previous_cursor addObject:str];
    
    self.next_cursor = [NSMutableArray array];
    [self.next_cursor addObject:str];
    
*/
    
    
    
}

- (void)AFN
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=%@",self.name1,self.name2];
//    NSLog(@"self.name1 = %@",self.name1);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
                        NSLog(@"111111111=%@", responseObject);
        self.popArray = [NSMutableArray array];
        
        NSMutableDictionary *classDic = responseObject;
        NSMutableArray *classArray = [classDic objectForKeyedSubscript:@"list"];
        for (NSMutableDictionary *dic in classArray) {
            ClassModel *model = [[ClassModel alloc] init];
            model.albumCoverUrl290 = [dic objectForKey:@"albumCoverUrl290"];
            model.lastUptrackAt = [dic objectForKey:@"lastUptrackAt"];
            model.playsCounts = [dic objectForKey:@"playsCounts"];
            model.title = [dic objectForKey:@"title"];
//            model.nickname
            model.aoId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
//            NSLog(@"%@",model.aoId);
            
            //            NSLog(@"sssss%@", model.ID);
            [self.popArray addObject:model];
        }
        
        [self.popularTableView1 reloadData];
        //                NSLog(@"self.array = %@", self.popArray);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@", error);
    }];
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndetifier = @"reuse";
    
    
    ClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier];
    if (cell == nil) {
        cell = [[ClassTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndetifier];
    }
    
    ClassModel *model = [self.popArray objectAtIndex:indexPath.row];
    cell.model = model;
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"u=213802053,255796087&fm=21&gp=0" ofType:@"jpg"]]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClassInformationViewController *classInformationVC =  [[ClassInformationViewController alloc] init];
    ClassModel *classModel = [self.popArray objectAtIndex:indexPath.row];
    classInformationVC.model = classModel;
    
    
    
    
    
    
    //    classInformationVC.model.ID = classModel.ID;
    //    NSLog(@"!+++%@", classModel);
    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:classInformationVC];
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:@"u=213802053,255796087&fm=21&gp=0" ofType:@"jpg"]]]];
    naVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];
//    tabBarController.tabBar.backgroundColor = [UIColor redColor];
    tabBarController.viewControllers = [NSArray arrayWithObjects:naVC, nil];
    [naVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [self presentViewController:tabBarController animated:NO completion:^{
        
    }];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    
    
    
    
    
//       [self.navigationController pushViewController:classInformationVC animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    return self.popArray.count;
    return self.popArray.count;
}


- (void)segmented:(UISegmentedControl *)segmented
{
    if (segmented.selectedSegmentIndex == 0) {
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=hot&device=iPhone&page=1&per_page=20&status=0&tag_name=%@",self.name1,self.name2];
//        NSLog(@"self.name1 = %@",self.name1);
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        //    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
        [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
//                    NSLog(@"111111111=%@", responseObject);
            self.popArray = [NSMutableArray array];
            
            NSMutableDictionary *classDic = responseObject;
            NSMutableArray *classArray = [classDic objectForKeyedSubscript:@"list"];
            for (NSMutableDictionary *dic in classArray) {
                ClassModel *model = [[ClassModel alloc] init];
                model.albumCoverUrl290 = [dic objectForKey:@"albumCoverUrl290"];
                model.lastUptrackAt = [dic objectForKey:@"lastUptrackAt"];
                model.playsCounts = [dic objectForKey:@"playsCounts"];
                model.title = [dic objectForKey:@"title"];
                model.aoId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];
                [self.popArray addObject:model];
                
            }
            
            [self.popularTableView1 reloadData];
            //        NSLog(@"self.array = %@", self.popArray);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"%@", error);
        }];
        
    }
    
    
    else if (segmented.selectedSegmentIndex == 1)
    {
        
        //        NSLog(@"11111111111111");
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        NSString *urlStr1 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=recent&device=iPhone&page=1&per_page=20&status=0&tag_name=%@", self.name1,self.name2];
        
        
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        [manager GET:urlStr1 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
//            NSLog(@"object = %@", responseObject);
            NSMutableDictionary *ClassDic = responseObject;
            self.popArray = [NSMutableArray array];
            NSMutableArray *array = [ClassDic objectForKey:@"list"];
            
            for (NSMutableDictionary *dic in array) {
                ClassModel *model = [[ClassModel alloc] init];
                model.albumCoverUrl290 = [dic objectForKey:@"albumCoverUrl290"];
                model.lastUptrackAt = [dic objectForKey:@"lastUptrackAt"];
                model.playsCounts = [dic objectForKey:@"playsCounts"];
                model.title = [dic objectForKey:@"title"];
                model.tracksCounts = [dic objectForKey:@"tracksCounts"];
                model.aoId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];

                [self.popArray addObject:model];
            }
            [self.popularTableView1 reloadData];
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        
    }
    else if (segmented.selectedSegmentIndex == 2)
    {
        AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
        
        NSString *urlStr2 = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/explore_album_list?category_name=%@&condition=classic&device=iPhone&page=1&per_page=20&status=0&tag_name=%@", self.name1,self.name2];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        
        [manager GET:urlStr2 parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [netWorkManager stopMonitoring];
//            NSLog(@"object1111 = %@", responseObject);
            NSMutableDictionary *ClassDic = responseObject;
            self.popArray = [NSMutableArray array];
            NSMutableArray *array = [ClassDic objectForKey:@"list"];
            
            for (NSMutableDictionary *dic in array) {
                ClassModel *model = [[ClassModel alloc] init];
                model.albumCoverUrl290 = [dic objectForKey:@"albumCoverUrl290"];
                model.lastUptrackAt = [dic objectForKey:@"lastUptrackAt"];
                model.playsCounts = [dic objectForKey:@"playsCounts"];
                model.title = [dic objectForKey:@"title"];
                model.tracksCounts = [dic objectForKey:@"tracksCounts"];
                model.aoId = [NSString stringWithFormat:@"%@", [dic objectForKey:@"id"]];

                [self.popArray addObject:model];
            }
            [self.popularTableView1 reloadData];
            
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            
        }];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
